<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text" id="talk">
    <div id="result"></div>
    <script>
        const talk = document.querySelector('#talk')
        const result = document.querySelector('#result')
        talk.onkeyup = (e) => {
            if (e.key === 'Enter') {
                const xhr = new XMLHttpRequest()
                xhr.open('GET', 'http://ajax-api.itheima.net/api/robot?spoken=' + talk.value)
                result.innerHTML += `<div>我：${talk.value}</div>`
                talk.value = null
                xhr.send()
                xhr.responseType = 'json'
                xhr.onreadystatechange = () => {
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        result.innerHTML += `<div>机器人：${xhr.response.data.info.text}</div>`
                    }
                }
            }
        }
    </script>
</body>

</html>